package com.ruoyi.qiushi.service.mall.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.qiushi.domain.system.QiushiMessage;
import com.ruoyi.qiushi.domain.mall.TradeOrder;
import com.ruoyi.qiushi.mapper.system.QiushiMessageMapper;
import com.ruoyi.qiushi.mapper.mall.TradeOrderMapper;
import com.ruoyi.qiushi.service.mall.ITradeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 普通商品订单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-08
 */
@Service
public class TradeOrderServiceImpl implements ITradeOrderService {
    @Autowired
    private TradeOrderMapper tradeOrderMapper;
    @Autowired
    private QiushiMessageMapper qiushiMessageMapper;

    /**
     * 查询普通商品订单
     *
     * @param id 普通商品订单主键
     * @return 普通商品订单
     */
    @Override
    public TradeOrder selectTradeOrderById(Long id) {
        return tradeOrderMapper.selectTradeOrderById(id);
    }

    /**
     * 查询普通商品订单列表
     *
     * @param tradeOrder 普通商品订单
     * @return 普通商品订单
     */
    @Override
    public List<TradeOrder> selectTradeOrderList(TradeOrder tradeOrder) {
        return tradeOrderMapper.selectTradeOrderList(tradeOrder);
    }

    /**
     * 新增普通商品订单
     *
     * @param tradeOrder 普通商品订单
     * @return 结果
     */
    @Override
    public int insertTradeOrder(TradeOrder tradeOrder) {
        tradeOrder.setCreateTime(DateUtils.getNowDate());
        return tradeOrderMapper.insertTradeOrder(tradeOrder);
    }

    /**
     * 修改普通商品订单
     *
     * @param tradeOrder 普通商品订单
     * @return 结果
     */
    @Override
    public int updateTradeOrder(TradeOrder tradeOrder) {
        tradeOrder.setStatus("3");
        tradeOrder.setDeliveryTime(new Date());
        tradeOrder.setUpdateTime(DateUtils.getNowDate());

        QiushiMessage qiushiMessage = new QiushiMessage();
        qiushiMessage.setUserId(tradeOrder.getUserId());
        qiushiMessage.setTitle("普通商品发货");
        qiushiMessage.setContent("普通商品订单“" + tradeOrder.getNo() + "”已发货，请及时确认收货");
        qiushiMessage.setType("3");
        qiushiMessage.setCreateTime(new Date());
        qiushiMessage.setUpdateTime(new Date());
        qiushiMessageMapper.insertQiushiMessage(qiushiMessage);

        return tradeOrderMapper.updateTradeOrder(tradeOrder);
    }

    /**
     * 删除普通商品订单信息
     *
     * @param id 普通商品订单主键
     * @return 结果
     */
    @Override
    public int deleteTradeOrderById(Long id) {
        return tradeOrderMapper.deleteTradeOrderById(id);
    }
}
